﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Ticket : System.Web.UI.Page
{
    Tickets tk = new Tickets();
    Product pd = new Product();
    public string Movie(object obj)
    {
        return pd.GetMovieNameById(obj.ToString());
    }
    public string Status(object obj)
    {
        if (0 == int.Parse(obj.ToString()))
        {
            return "No sale";
        }
        else
        {
            return "Sold";
        }

    }
    public static int m_se = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(this.IsPostBack ==  false)
        {
            txtTicketId.Text = tk.GetTicketID();
            DateTime day = new DateTime();
            day = DateTime.Now.Date;
            ddlMovieID.DataSource = pd.GetMovieAll(day);
            ddlMovieID.DataTextField = "MovieName";
            ddlMovieID.DataValueField = "MovieID";
            ddlMovieID.DataBind();
            ddlSearch.DataSource = pd.GetMovieAll(day);
            ddlSearch.DataTextField = "MovieName";
            ddlSearch.DataValueField = "MovieID";
            ddlSearch.DataBind();
            ddlChairID.DataSource = tk.LoadChair();
            ddlChairID.DataTextField = "ChairID";
            ddlChairID.DataValueField = "ChairID";
            ddlChairID.DataBind();
            grvData.DataSource = tk.LoadTicket();
            grvData.DataBind();
        }
    }
    protected void btnSummit_Click(object sender, EventArgs e)
    {
        if(tk.AddTicket(tk.GetTicketID(),ddlMovieID.SelectedValue.ToString(),ddlChairID.SelectedValue.ToString(),float.Parse(txtPrice.Text.Trim()),DateTime.Parse(txtDateMovie.Text.Trim()),DateTime.Parse(txtTimeMovie.Text.Trim()),int.Parse(ddlStatus.SelectedValue.ToString()),txtNotes.Text))
        {
            txtTicketId.Text = tk.GetTicketID();
            txtNotes.Text = null;
        }
    }
    protected void btnSearch0_Click(object sender, EventArgs e)
    {
        if ("" == txtSearch.Text.Trim())
        {
            if ("" == txtTimeMovieS.Text.Trim())
            {
                grvData.DataSource = tk.LoadTicketByMovie(ddlSearch.SelectedValue.ToString());
                grvData.DataBind();
                m_se = 1;
            }
            else
            {
                lblError1.Visible = true;
                lblError1.Text = "Select date!";
            }
        }
        else
        {
            if ("" == txtTimeMovieS.Text.Trim())
            {
                grvData.DataSource = tk.LoadTicketByDate(ddlSearch.SelectedValue.ToString(),DateTime.Parse(txtSearch.Text.Trim()));
                grvData.DataBind();
                m_se = 2;
            }
            else
            {
                grvData.DataSource = tk.LoadTicketByDateAndTime(ddlSearch.SelectedValue.ToString(), DateTime.Parse(txtSearch.Text.Trim()),DateTime.Parse(txtTimeMovieS.Text.Trim()));
                grvData.DataBind();
                m_se = 3;
            }
        }

    }
    protected void grvIndex(object sender, GridViewPageEventArgs e)
    {
        grvData.PageIndex = e.NewPageIndex;
        if(m_se==0)
        {
            grvData.DataSource = tk.LoadTicket();
            grvData.DataBind();
        }
        else if(m_se==1)
        {
            grvData.DataSource = tk.LoadTicketByMovie(ddlMovieID.SelectedValue.ToString());
            grvData.DataBind();
        }
        else if(m_se==2)
        {
            grvData.DataSource = tk.LoadTicketByDate(ddlMovieID.SelectedValue.ToString(), DateTime.Parse(txtSearch.Text.Trim()));
            grvData.DataBind();
        }
        else if (m_se == 3)
        {
            grvData.DataSource = tk.LoadTicketByDateAndTime(ddlMovieID.SelectedValue.ToString(), DateTime.Parse(txtSearch.Text.Trim()), DateTime.Parse(txtTimeMovieS.Text.Trim()));
            grvData.DataBind();
        }
    }
    protected void grvDataCancel(object sender, GridViewCancelEditEventArgs e)
    {
        grvData.EditIndex = -1;
        if (m_se == 0)
        {
            grvData.DataSource = tk.LoadTicket();
            grvData.DataBind();
        }
        else if (m_se == 1)
        {
            grvData.DataSource = tk.LoadTicketByMovie(ddlMovieID.SelectedValue.ToString());
            grvData.DataBind();
        }
        else if (m_se == 2)
        {
            grvData.DataSource = tk.LoadTicketByDate(ddlMovieID.SelectedValue.ToString(), DateTime.Parse(txtSearch.Text.Trim()));
            grvData.DataBind();
        }
        else if (m_se == 3)
        {
            grvData.DataSource = tk.LoadTicketByDateAndTime(ddlMovieID.SelectedValue.ToString(), DateTime.Parse(txtSearch.Text.Trim()), DateTime.Parse(txtTimeMovieS.Text.Trim()));
            grvData.DataBind();
        }
    }
    protected void grvDataEdit(object sender, GridViewEditEventArgs e)
    {
        grvData.EditIndex = e.NewEditIndex;
        if (m_se == 0)
        {
            grvData.DataSource = tk.LoadTicket();
            grvData.DataBind();
        }
        else if (m_se == 1)
        {
            grvData.DataSource = tk.LoadTicketByMovie(ddlMovieID.SelectedValue.ToString());
            grvData.DataBind();
        }
        else if (m_se == 2)
        {
            grvData.DataSource = tk.LoadTicketByDate(ddlMovieID.SelectedValue.ToString(), DateTime.Parse(txtSearch.Text.Trim()));
            grvData.DataBind();
        }
        else if (m_se == 3)
        {
            grvData.DataSource = tk.LoadTicketByDateAndTime(ddlMovieID.SelectedValue.ToString(), DateTime.Parse(txtSearch.Text.Trim()), DateTime.Parse(txtTimeMovieS.Text.Trim()));
            grvData.DataBind();
        }
    }
    protected void grvDataUpload(object sender, GridViewUpdateEventArgs e)
    {
        if (tk.UpdateTicket(grvData.DataKeys[e.RowIndex].Value.ToString(), (grvData.Rows[e.RowIndex].Cells[2].Controls[0] as DropDownList).SelectedValue.ToString(), float.Parse((grvData.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text), DateTime.Parse((grvData.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox).Text), DateTime.Parse((grvData.Rows[e.RowIndex].Cells[5].Controls[0] as TextBox).Text), int.Parse((grvData.Rows[e.RowIndex].Cells[6].Controls[0] as TextBox).Text), (grvData.Rows[e.RowIndex].Cells[7].Controls[0] as TextBox).Text))
        {
            if (m_se == 0)
            {
                grvData.DataSource = tk.LoadTicket();
                grvData.DataBind();
            }
            else if (m_se == 1)
            {
                grvData.DataSource = tk.LoadTicketByMovie(ddlMovieID.SelectedValue.ToString());
                grvData.DataBind();
            }
            else if (m_se == 2)
            {
                grvData.DataSource = tk.LoadTicketByDate(ddlMovieID.SelectedValue.ToString(), DateTime.Parse(txtSearch.Text.Trim()));
                grvData.DataBind();
            }
            else if (m_se == 3)
            {
                grvData.DataSource = tk.LoadTicketByDateAndTime(ddlMovieID.SelectedValue.ToString(), DateTime.Parse(txtSearch.Text.Trim()), DateTime.Parse(txtTimeMovieS.Text.Trim()));
                grvData.DataBind();
            }
        }
    }
}